package com.cgw.game.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.cgw.game.R;

import java.util.ArrayList;

/**
 * app自定义仪表盘
 * */
public class DashboardView extends View {
    /**
     * 画笔
     * */
    private Paint mPaint;
    /**
     * 文字画笔
     * */
    private Paint mTextPaint;
    /**
     * 背景画笔
     * */
    private Paint mBgPaint;
    /**
     * 宽
     * */
    private int width;
    /**
     * 高
     * */
    private int height;
    /**
     * 弧线的宽度
     * */
    private int arcStrokeWidth = 40;
    /**
     * 弧线的间距
     * */
    private float spacingWidth = 9;
    private Context context;
    /**
     * 顶部title
     * */
    private String[] titles = new String[]{"睡眠","体能","节律","心率","血氧","情绪","认知","血压"};
    /**
     * 点击区域集合
     * */
    private ArrayList<RectF> touchRect = new ArrayList<>();
    /**
     * 当前选中的
     * */
    private int pos = 0;
    /**
     * 监听接口
     * */
    private OnTitleClickListener listener;
    /**
     * 背景色集合
     * */
    private String[] colorBg = new String[]{
            "#2B3195","#716EE3",
            "#E68F41","#EEB055",
            "#689EF2","#6F72E5",
            "#D45C57","#FF9D5E",
            "#D45C57","#FF9D5E",
            "#8261F3","#9E85F4",
            "#44A9CD","#7ED2F5",
            "#D45C57","#FF9D5E",
    };
    private int icons[] = new int[]{
            R.mipmap.sleep_icon,
            R.mipmap.stamina_icon,
            R.mipmap.joint_rate_icon,
            R.mipmap.heart_rate_icon,
            R.mipmap.blood_oxygen_icon,
            R.mipmap.emotion_icon,
            R.mipmap.intelligence_icon,
            R.mipmap.blood_pressure_icon,
    };
    /**
     * 中心值
     * */
    private String centreValue = "——";
    /**
     * 中心单位
     * */
    private String centreCompany = "";
    /**
     * 底部值
     * */
    private String bottomValue = "暂无数据";

    /**
     * 图像
     * */
    private Bitmap bitmap;
    /**
     * 图像高度
     * */
    private int bitmapHeight;
    /**
     * 图像宽度
     * */
    private int bitmapWidth;

    public DashboardView(Context context) {
        this(context,null);
    }

    public DashboardView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public DashboardView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }
    private void initView() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(5);
        //
        mBgPaint = new Paint();
        mBgPaint.setAntiAlias(true);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(Color.WHITE);
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.home_top_bg);
        bitmapHeight = bitmap.getHeight();
        bitmapWidth = bitmap.getHeight();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width = measureSize(widthMeasureSpec);
        height = measureSize(heightMeasureSpec);
        setMeasuredDimension(width,height);
    }
    private int measureSize(int measureSpec) {
        int length;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if(mode == MeasureSpec.EXACTLY){
            length = size;
        }else{
            length = DensityUtil.dip2px(context,200);
            if(mode == MeasureSpec.AT_MOST){
                length = Math.min(length,size);
            }
        }
        return length;
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                float x = event.getX();
                float y = event.getY();
                if(touchRect.size()!=0){
                    for (int i = 0; i < touchRect.size(); i++) {
                        if(touchRect.get(i).contains(x,y)){
                            pos = i;
                            if(listener!=null){
                               listener.onTouchPos(pos);
                            }
                            invalidate();
                            return true;
                        }
                    }
                }
                return super.onTouchEvent(event);
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBg(canvas);
        drawBgBitmap(canvas);
        drawDashboard(canvas);
        drawBottomText(canvas);
        drawBottomBitmap(canvas);
        drawTopText(canvas);
    }



    /**
     * 画背景
     * */
    private void drawBg(Canvas canvas){
        LinearGradient gradient = new LinearGradient(0,0,width,height,
                Color.parseColor(colorBg[pos*2]),Color.parseColor(colorBg[pos*2+1]), Shader.TileMode.CLAMP);
        mBgPaint.setShader(gradient);
        canvas.drawRect(0,0,width,height,mBgPaint);
    }
    /**
     * 画背景的图片
     * */
    private void drawBgBitmap(Canvas canvas) {
        canvas.drawBitmap(bitmap,0,height-bitmapHeight,mBgPaint);
    }
    /**
     * 画仪表盘
     * */
    private void drawDashboard(Canvas canvas){
        mPaint.setARGB(200,255,255,255);
        canvas.save();
        //这里270的意思是旋转270的弧线的意思
        //(float) (width/2 + width/2 * Math.cos(135f*Math.PI/180))
        // 计算的是 135度的情况下 圆上的点位置  计算两个同一半径直线上的两个点画线 旋转画布角度  重复画  就达到了所有的刻度
        //这里的高度-20把圆形的高度拉上去一点
        float r = width/3-50;
        for (float i = 0; i < 200/spacingWidth; i++) {
            canvas.drawLine(
                    (float) (width/2 + r * Math.cos(171f*Math.PI/180)),
                    (float) (height-DensityUtil.dip2px(context,30) + (r) * Math.sin(171f*Math.PI/180)),
                    (float) (width/2 + (r-arcStrokeWidth) * Math.cos(171f*Math.PI/180)),
                    (float) (height-DensityUtil.dip2px(context,30) + (r-arcStrokeWidth) * Math.sin(171f*Math.PI/180)),
                    mPaint);
            canvas.rotate(spacingWidth, getWidth() / 2, height-DensityUtil.dip2px(context,30));
        }
        canvas.restore();
    }

    /**
     * 画底部文字
     * */
    private void drawBottomText(Canvas canvas){
        mTextPaint.setAlpha(200);
        mTextPaint.setTextSize(DensityUtil.dip2px(context,12));
        float textWidth = mTextPaint.measureText(bottomValue);
        canvas.drawText(bottomValue,width/2-textWidth/2,height-DensityUtil.dip2px(context,10),mTextPaint);
        /**
         * 如果存在单位的话  字号需要进行缩小
         * */
        if(TextUtils.isEmpty(centreCompany)){
             mTextPaint.setTextSize(DensityUtil.dip2px(context,27));
            float textContentWidth = mTextPaint.measureText(centreValue);
            canvas.drawText(centreValue,width/2-textContentWidth/2,height-((width/3-50)+DensityUtil.dip2px(context,30))/2+DensityUtil.dip2px(context,25),mTextPaint);
        }else{
            mTextPaint.setAlpha(255);
            mTextPaint.setTextSize(DensityUtil.dip2px(context,27));
            float textContentWidth1 = mTextPaint.measureText(centreValue);
            mTextPaint.setTextSize(DensityUtil.dip2px(context,12));
            float textContentWidth3 = mTextPaint.measureText(centreCompany);
            mTextPaint.setTextSize(DensityUtil.dip2px(context,27));
            canvas.drawText(centreValue,width/2-textContentWidth1/2-textContentWidth3/2,height-((width/3-50)+DensityUtil.dip2px(context,30))/2+DensityUtil.dip2px(context,25),mTextPaint);
            mTextPaint.setTextSize(DensityUtil.dip2px(context,12));
            mTextPaint.setAlpha(200);
            canvas.drawText(centreCompany,width/2+textContentWidth1/2-textContentWidth3/2,height-((width/3-50)+DensityUtil.dip2px(context,30))/2+DensityUtil.dip2px(context,25),mTextPaint);
        }
    }
    /**
     * 画顶部图片
     * */
    private void drawBottomBitmap(Canvas canvas){
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),icons[pos]);
        int bitmapHeight = bitmap.getHeight();
        int bitmapWidth = bitmap.getHeight();
        float btmHeight = height-(width/3-50+DensityUtil.dip2px(context,30))*2/3;
        canvas.drawBitmap(bitmap,width/2-bitmapWidth/2,btmHeight-bitmapHeight/2,mPaint);
    }
    /**
     * 画外圈汉字
     * */
    private void drawTopText(Canvas canvas){
        for (int i = 0; i < titles.length; i++) {
            if(pos!=i){
                mTextPaint.setAlpha(200);
                mTextPaint.setTypeface(Typeface.DEFAULT);
                mTextPaint.setTextSize(DensityUtil.dip2px(context,14));
            }else{
                mTextPaint.setAlpha(255);
                mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
                mTextPaint.setTextSize(DensityUtil.dip2px(context,18));
            }
            //文字区域测量
            Rect rect = new Rect();
            mTextPaint.getTextBounds(titles[i], 0, titles[i].length(), rect);
            float angle = i*20;
            if(i>=4){
                angle = angle+20;
            }
            float x =  (float) (width/2 + width/2.5 * Math.cos((190f+angle)*Math.PI/180));
            float y = (float) (height-DensityUtil.dip2px(context,15) + (width/2.5) * Math.sin((190f+angle)*Math.PI/180));
            canvas.drawText(titles[i],x-rect.width()/2,y-rect.height()/2,mTextPaint);
            if(touchRect.size()<8){
                //点击区域存储
                RectF tonch = new RectF();
                //加5dp的容错
                tonch.set(x-rect.width()/2-DensityUtil.dip2px(context,8),y-rect.height()/2-DensityUtil.dip2px(context,8),x+rect.width()/2+DensityUtil.dip2px(context,5),y+rect.height()/2+DensityUtil.dip2px(context,5));
                touchRect.add(tonch);
            }
        }
    }
    public interface OnTitleClickListener{
        void onTouchPos(int pos);
    }

    public void setListener(OnTitleClickListener listener) {
        this.listener = listener;
    }

    public void setValue(String centreValue,String centreCompany,String bottomValue) {
        this.centreValue = centreValue;
        this.bottomValue = bottomValue;
        this.centreCompany = centreCompany;
        invalidate();
    }
    public void setValue(String centreValue,String bottomValue) {
        this.centreValue = centreValue;
        this.bottomValue = bottomValue;
        this.centreCompany = "";
        invalidate();
    }
}
